import pygame
import random
from Settings import *
import sys
from basic_class import *
from Sprites import *



g_window = pygame.display.set_mode((1010,600))  # 窗口大小



def tuple_sub(a, b):
    return (a[0] - b[0], a[1] - b[1])
    # 有一种更好的写法是 return tuple(i - j for i, j in zip(a, b))，不同的运算只要修改中间的符号即可


def tuple_mul(a, b):
    return (a[0] * b, a[1] * b)


def tuple_min(a, b):
    return (min(a[0], b[0]), min(a[1], b[1]))


def tuple_max(a, b):
    return (max(a[0], b[0]), max(a[1], b[1]))



      
class Scene():
    def __init__(self,player):
        self.player = player
        self.camera = (0,0)
        self.window_scale = (1010, 600)  # 显示窗口的大小
        self.map_range = (4000, 1000)
          # 实际地图的大小
        self.update_camera()

                   
    
                  
    def update_camera(self):
        player_center_cord = (
            self.player.pos
        )  # 获取玩家当前的位置，使用center是因为玩家有60*60的大小，要获取中间位置
        self.camera = tuple_sub(
            player_center_cord, tuple_mul(self.window_scale, 0.5)
        )  # 镜头的左上角位置应该是玩家的位置减去一半窗口的宽和高
        left_top = (0, 0)  # 镜头的横纵坐标不能低于(0, 0)
        right_down = tuple_sub(
            self.map_range, self.window_scale
        )  # 镜头的横纵坐标不能多于地图大小减去窗口大小
        # 对镜头的位置和上述两点分别取min和max，防止镜头超出地图范围
        self.camera = tuple_min(right_down, self.camera)
        self.camera = tuple_max(left_top, self.camera)

    '''def listen(self, event: Event):  # 场景所监听的事件
        super().listen(event)
        if event.code == REQUEST_MOVE:  # 监听玩家的移动请求事件
            can_move = 1  # 一开始默认可以移动
            target_rect = pygame.Rect(
                event.body["POS"][0], event.body["POS"][1], 60, 60
            )  # 获得移动后的玩家矩形
            for wall in self.walls:  # 遍历所有障碍物
                if wall.rect.colliderect(target_rect):  # 调用pygame的方法检测碰撞
                    can_move = 0  # 有碰撞则不能移动
                    break  # 有碰撞时不用再判断后面的了
                if MapSettings.matrix[event.body["POS"][1]//40][event.body["POS"][0]//40] == 0 or MapSettings.matrix[event.body["POS"][1]//40][event.body["POS"][0]//40] == 1:
                    can_move = 0
                    break
            for monster in self.monsters:
                if monster.rect.colliderect(target_rect):
                    can_move = 0
                    break    
            if can_move:  # 如果可以移动的话就发送允许移动事件

                self.post(Event(CAN_MOVE, event.body))
        elif event.code == REQUEST_MOVE_MONSTER:
            can_move = 1
            target_rect = pygame.Rect(
                event.body["POS"][0], event.body["POS"][1], 60, 60
            )
            for wall in self.walls:
                if wall.rect.colliderect(target_rect):
                    can_move = 0
                    break
            if self.player.rect.colliderect(target_rect):
                can_move = 0    
            if can_move:
                self.post(Event(CAN_MOVE_MONSTER, event.body))        

        if event.code == STEP:  # STEP是每次游戏周期刷新时会被触发的事件
            self.update_camera()  # 更新镜头的位置

        if event.code == DRAW:  # DRAW事件，用于描绘场景中的实体
            for tile in self.tiles:  # 遍历所有地图背景图块并描绘
                tile.draw(self.camera)
            for wall in self.walls:  # 遍历所有障碍物并描绘
                wall.draw(self.camera)
            self.player.draw(self.camera)  # 描绘玩家图像
            for monster in self.monsters:
                monster.draw(self.camera)
            self.npc.draw(self.camera)    '''
